package com.chinasofti.etc.alcohol.web;


import com.chinasofti.etc.alcohol.common.NumberGenerator;
import com.chinasofti.etc.alcohol.dao.model.User;
import com.chinasofti.etc.alcohol.service.IUserService;
import com.chinasofti.etc.alcohol.web.form.LoginForm;
import com.chinasofti.etc.alcohol.web.form.RegisterForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  用户操作相关的控制器
 * </p>
 *
 * @author lucas
 * @since 2019-05-28
 */
@Controller
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String toLogin(){
        return "login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String doLogin(LoginForm form, ModelMap modelMap, HttpServletRequest request, HttpSession session){
        String message = null;
        User user =userService.queryOne(form.getUsername());
        session.setAttribute("user",user);
        UsernamePasswordToken token = new UsernamePasswordToken(form.getUsername(),form.getPassword());
        token.setRememberMe(false);

        Subject currUser = SecurityUtils.getSubject();
        try {
            currUser.login(token);
            SavedRequest req = WebUtils.getAndClearSavedRequest(request);
            if(req==null){
                return "redirect:/";
            }else {
                String requestUrl = req.getRequestUrl();
                return "redirect:" + requestUrl;
            }
        }catch (IncorrectCredentialsException e){
            log.info("用户名或者密码错误");
            message="用户名或者密码错误";
        }catch (AuthenticationException e){
            log.warn("登录失败",e);
            message="登录失败";
        }
        modelMap.addAttribute("message",message);
        return "login";
    }


    @RequestMapping(value = "register",method = RequestMethod.GET)
    public String toRegister(){
        return "register";
    }

    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String doRegister(RegisterForm form){
        log.info("{}",form);


        User u = new User();
        u.setUserDate(LocalDateTime.now());
        u.setUserName(form.getUsername());
        u.setUserPassword(form.getPassword());
        u.setUserNumber(NumberGenerator.genUserNumer());
        u.setUserTel(form.getTel());
        userService.save(u);

        return "redirect:login";
    }

}

